#include"Queue.h"

int InitQueue(Queue* qu) {
	qu->len = 0;
	qu->head = NULL;
	//(QPtr)calloc(1, sizeof(QNode))
	qu->tail = NULL;
	return 0;
}
int EnqueueEle(Queue* qu,void* dataAddress,int tp_size) {
	QPtr p = (QPtr)calloc(1, sizeof(QNode));
	if (!(p->data = (void*)calloc(1, tp_size)))
	{
		return -1;
	}
	memcpy(p->data, dataAddress, tp_size);
	p->next = NULL;
	if (qu->len)
	{
		qu->tail->next = p;
		qu->tail = p;
	}
	else
	{
		qu->head = p;
		qu->tail = p;
	}
	qu->len++;
	return 0;
	
}

int DequeueEle(Queue* qu, void* dataAddress, int tp_size) {
	if (qu->len>0)
	{
		QPtr p = qu->head;
		if (qu->len == 1)
		{
			qu->tail = NULL;
		}
		qu->head = qu->head->next;
		qu->len--;
		memcpy(dataAddress, p->data, tp_size);
		free(p->data);
		free(p);
	}
	return 0;
}

QPtr SeekQueueEle(Queue qu, void* dataAddress, int tp_size) {
	if (qu.len==0)
	{
		return NULL;
	}
	QPtr p = qu.head;
	while (p) {
		if (!memcmp(p->data, dataAddress, tp_size))
		{
			return p;
		}
		p = p->next;
	}
	return NULL;
}

//int PrintQueue(Queue qu) {
//	QPtr p = qu.head;
//	while (p) {
//		char* a = (char*)(p->data);
//		printf("%c\n", *a);
//		p = p->next;
//	}
//	return 0;
//}
//int main() {
//	Queue q ;
//	InitQueue(&q);
//	char a[4] = "zha";
//	for (int i = 0; i < 3; i++)
//	{
//		printf("%c %d\n", a[i], sizeof(char));
//		EnqueueEle(&q, &a[i], sizeof(char));
//	}
//	char c;
//	DequeueEle(&q,&c, sizeof(char));
//	printf("Dequeue:%c\n", c);
//	PrintQueue(q);
//	QPtr tmp=SeekQueueEle(q, &a[2], sizeof(char));
//	char* t = (char*)(tmp->data);
//	printf("seek:%c\n", *t);
//}